package main

import (
	"encoding/base64"
	"fmt"
	"net/url"
)

func main() {
	/*
		Base64/URLEncode在网络程序开发中经常用到，下面是他们的例子。
	*/

	fmt.Println("Base64和URLEncode编码解码")

	var byteArray = [...]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	fmt.Println("测试二进制数组:", byteArray)
	str := base64.StdEncoding.EncodeToString(byteArray[:])
	fmt.Println("Base64编码后:", str)

	ba, err := base64.StdEncoding.DecodeString(str)
	if err != nil {
		fmt.Println("在执行解码时发生错误:", err.Error())
	} else {
		fmt.Println("字符串解码为二进制切片:", ba)
	}

	oriStr := "k=kkkk&v={\"okkk\":\"ovvv\"}"
	fmt.Println("测试URLEncode,原始字符串:", oriStr)
	urle := url.QueryEscape(oriStr)
	fmt.Println("执行URLEncode后:", urle)
	urld, err := url.QueryUnescape(urle)
	if err != nil {
		fmt.Println("执行URLDecode发生错误:", urld)
	} else {
		fmt.Println("执行URLDecode后:", urld)
	}
}
